<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use app\admin\model\Member as MemberModel;
use app\web\model\ComplaintModel;
use think\Db;
use think\Session;
use think\Cookie;
class MemberController extends IndexBaseController {
	protected function _initialize() {
			 parent::_initialize();
	}
	/**
	 * 获取代理推广信息
	 */
	public function getTgzx(){
		
		$list_1 =db::name("mall_level")->field('son_id')->where('uid',input("uid"))->select();
		$list_2 =db::name("mall_level")->field('son_id')->where('uid','in',$list_1[0]['son_id'])->select();
		$list_3 =db::name("mall_level")->field('son_id')->where('uid','in',$list_2[0]['son_id'])->select();
		
		//线下总会员数
		$user_total = count($list_1)+count($list_2)+count($list_3);
		
		//今日分佣 
		$sql = "select sum(price) as price  from long_mall_credits where uid = ".input("uid")." and credittype = 'credit2' and type = 1 and str_to_date(add_time,'%Y-%m-%d') = str_to_date(NOW(),'%Y-%m-%d')";
		$now_brokerage=Db::query($sql);
		
		//总佣金
		$total_brokerage = $user_total*2;
		
		$data['user_total'] = $user_total;
		$data['now_brokerage'] = $now_brokerage[0]['price'];
		$data['total_brokerage'] = $total_brokerage;
		
		return $this->toJson(200, '', $data);
		
		/*$where['level']=1;
		$sons=db("mall_level")->where($where)->count();
		$sql = 'SELECT sum(price) as price FROM long_mall_credits WHERE uid = '.input("uid").' AND price > 0 AND credittype = "credit2" AND type = 1 AND add_time >= '.date('Y-m-d');
		$res=Db::query($sql);
		$data['sons']=$sons;
		$data['money']=$res[0]['price']?$res:0;
		return $this->toJson(200, '', $data);*/
	}
	/**
	 * 会员中心首页数据
	 */
	public function get_data(){
		$where['uid']=input("uid");
		$collects=db("mall_collect")->where($where)->count("id");
		$foots=db("mall_foot")->where($where)->count("id");
		return json(["colls"=>$collects,"foots"=>$foots]);
	}
	//商品兑换
	public function do_exchange(){
		$goods=db("line_goods")->find(input("goods_id"));
		$member=db("member")->find(input("uid"));
		$exechage_score=$goods['exchange_bouns'];//兑换积分
		if($member['credit1']<$exechage_score){
			return json(["code"=>400,"msg"=>'积分不足']);
		}
		$address_info = Db::name("mall_address")->where('uid', input("uid"))->where('isdefault',2)->find();
		if(!$address_info){
			$address_info = Db::name("mall_address")->where('uid', input("uid"))->find();
		}
		$order['user_id']=input("uid");
		$order['pay_type']=1;
		$order['order_time']=now_time();
		$order['pay_time']=now_time();
		$order['need_score']=$goods['exchange_bouns'];
		$order['total_price']=0;
		$order['pay_price']=0;
		$order['pay_time']=now_time();
		$order['order_state']=3;
		$order['sn']=order_create_no();
		$order['receiver']=$address_info['realname'];
		$order['receiver_mobile']=$address_info['mobile'];
		$order['city']=$address_info['city'];
		$order['province']=$address_info['province'];
		$order['district']=$address_info['district'];
		$order['address']=$address_info['address'];
		$res=db("mall_order")->insert($order);
		$order_id = db("mall_order")->getLastInsID();
		$item['order_id']=$order_id;
		$item['goods_id']=input("goods_id");
		$item['goods_name']=$goods['goods_name'];
		$item['price']=0;
		$item['need_score']=$goods['exchange_bouns'];
		$item['number']=1;
		$item['total_price']=0;
		$res=db('mall_order_item')->insert($item);
		if($res){
			$data = array(
				'uid' => $member['id'],
				'credittype' => "credit1",
				'price' =>$goods['exchange_bouns'],
				'add_time' => now_time(),
				'operator' => 0,
				'remark' => '积分兑换扣除积分',
			    'type' => 1,
			    'tid' => 0,
			    "is_pt"=>0,
			    "tid"=>$order_id
			);
			db('mall_credits')->insert($data);
			$res3=db('member')->where('id',input("uid"))->setDec("credit1",$goods['exchange_bouns']);
			if($res3){
				return json(["code"=>200,'msg'=>"兑换成功"]);
			}else{
				return json(["code"=>400,'msg'=>"兑换失败"]);
			}
		}
		return json(["code"=>200,'msg'=>'兑换成功']);
		
	}
	//积分商城商品兑换
	public function get_exchange_info(){
		$goods=db("line_goods")->find(input("goods_id"));
		$address_info = Db::name("mall_address")->where('uid', input("uid"))->where('isdefault',2)->find();
		$exechage_score=$goods['exchange_bouns'];//兑换积分
		if(!$address_info){
			$address_info = Db::name("mall_address")->where('uid', input("uid"))->find();
		}
		return json(["cod"=>200,'goods'=>$goods,'address_info'=>$address_info]);
		
	}
	/**
	 * 发布说说
	 */
	public function fb_ss(){
		$data["uid"]=input("uid");
		$data["content"]=input("content");
		$data["thumb"]=input("thumb");
		$data["add_time"]=now_time();
		$member=db("member")->find(input("uid"));
		$data['name']=$member['nick_name']?$member['nick_name']:$member['mobile'];
		$res=db('mall_assess')->insert($data);
		if($res){
			return json(["code"=>200,"msg"=>"发布成功"]);
		}else{
			return json(["code"=>406,"msg"=>"发布失败"]);
		}
	}
	/**
	 * 显示会员资料
	 */
	public function member_info() {
		$id = request() -> param('uid');
		$member = Db::name('member')->where('id='.$id)->find();
		if($member){
			return $this->toJson(200, 'ok', $member);
		}else{
			return $this->toJson(405, '',"数据异常");
		}
	}
	/**
	 * POST
	 * 修改用户
	 */
	public function update(){
		if($this->request->isPost()){
        	$member_model = new MemberModel();
			$data=input("post.");
			$result = $member_model -> allowField(true) -> save($data,$data['id']);
			if($result){
				return $this->toJson(200, '保存成功');
			}else{
				return $this->toJson(406, '保存失败');
			}
		}
	}
	//我的收藏
	public function getcolls(){
		$where=" where   c.uid = ".input("uid");
		if(input("type")){
			$where .=" and c.type=".input("type");
		}
		$sql="select c.uid, g.id, g.goods_name,g.comment,g.thumb,g.sale_price  from long_mall_collect c left join long_line_goods g on g.id=c.collect_id ".$where;
		$list= Db::query($sql);
		return json(["list"=>$list]);
	}
	//我的收藏
	public function getzjs(){
		$where=" where   c.uid = ".input("uid");
		$sql="select c.uid, g.id, g.goods_name,g.comment,g.thumb,g.sale_price  from long_mall_foot c left join long_line_goods g on g.id=c.goods_id ".$where;
		$list= Db::query($sql);
		return json(["list"=>$list]);
	}
	/**
 	 * POST
	 * 会员登录
	 */
	public function login(){
		$_SESSION["test"]="test";
        $where['mobile'] = input('mobile');
		$where['password'] = md5_salt(input('login_pwd'));
		$member_model = new MemberModel();
        $member = $member_model->where($where)->find();
        if (!empty($member)) {
            if ($member['status'] != 1) {
                return $this->toJson(408, '用户已禁用');
            } else {
                Db::name('member')->update(
                    [
                        'login_time' => now_time(),
                        'login_ip'   => $this->request->ip(),
                        'id'              => $member['id']
                    ]
                );
				$token=md5_salt($member['id'],config("salt"));
				$member['token']=$token;

				return $this->toJson(200, '登录成功', $member);
            }
        } else {
			return $this->toJson(408, '用户名或密码错误');
        }
	}
	
	/**
 	 * POST
	 * 会员快速登录
	 */
	public function loginfast(){

		$_SESSION["test"]="test";
		
        $where['mobile'] = input('mobile');
		$member_model = new MemberModel();
        $member = $member_model->where($where)->find();
        if (!empty($member)) {
            if ($member['status'] != 1) {
                return $this->toJson(408, '用户已禁用');
            } else {
                Db::name('member')->update(
                    [
                        'login_time' => now_time(),
                        'login_ip'   => $this->request->ip(),
                        'id'              => $member['id']
                    ]
                );
				$token=md5_salt($member['id'],config("salt"));
				$member['token']=$token;
				return $this->toJson(200, '登录成功', $member);
            }
        } else {
			return $this->toJson(408, '用户名或密码错误');
        }
	}
	
	//获取token
	public function getToken(){
		$appid=Config::get("appid");
		$appsecret=Config::get("appsecret");
		$weixin =  file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret');
		$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
		$array = get_object_vars($jsondecode);//转换成数组
		return $array['access_token'];
	}
	//打电话
	public function call(){
		$appid='3267';
		$app_key='api123';
		$url="http://101.200.199.55/webapi.php?";
		$redirect_url = urlencode('http://' . $_SERVER['HTTP_HOST'] . "/api/member/call_back");
		if(!input("uid")){
			return $this->toJson(406, '请求参数错误缺少用户id'); 
		}
		$member=db('member')->find(input("uid"));
		$mynumber=$member['mobile'];
		$number=input("number");
		if(!$number){
			return $this->toJson(406, '请求参数错误缺少拨打号码'); 
		}
		$score=$member['credit1'];
		$max=0;
		if($score>0){
			$max=$score/0.2;
		}else{
			return $this->toJson(406, '用户积分不足无法拨打电话'); 
		}
		$sign=md5("&appid=$appid&callnumber=$number&feetype=1&mynumber=$mynumber&params=1234&return_url=$redirect_url$app_key");
		$baseUrl=$url."act=call&appid=$appid&callnumber=$number&fee_type=1&max=$max&mynumber=$mynumber&params=1234&return_url=$redirect_url&sign=$sign";
		Header("Location: $baseUrl");
		exit;
	}
	public function call_back(){
		$status=input("state");
		echo $status;
		exit;
		if($status==1){
			$msg="您本次通话".input("fee_time")."分钟".input("Hold_time")."秒";
			return $this->toJson(200, $msg);
		}else{
			return $this->toJson(408, input("msg"));
		}
		
	}
	//获取openid
	function getOpenId(){
		$we_config = get_config('payment_config');
//		define("APPID", $we_config['wxpay_appid']);
//		define("MCHID", $we_config['wxpay_mchid']);
//		define("KEY", $we_config['wxpay_key']);
//		define("APPSECRET", $we_config['wxpay_appsercet']);
		$code = input("code");//获取code
		$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$we_config['wxpay_appid']."&secret=".$we_config['wxpay_appsercet']."&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
		$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
		$array = get_object_vars($jsondecode);//转换成数组
		$openid = $array['openid'];//输出openid
		
//		$access_token=$this->getToken();
//		$res=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN");
//		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
//		$array1 = get_object_vars($jsondecode1);//转换成数组
//		$nickname=$array1['nickname'];//昵称
//		$sex=$array1['sex'];//昵称
//		$headimgurl=$array1['headimgurl'];//微信头像
//		$city=$array1['city'];//城市
//		$province=$array1['province'];//省份
//		$country==$array1['country'];//国家
//		$where['openid']=$openid;
//		$member=db('member')->$where->find();
		if($openid){
			 Db::name('member')->update(
                    [
                        'openid'   => $openid,
                        'id'       => input("uid")
                    ]
             );
			 $res=array("code"=>200,'msg'=>"",'data'=>$oepnid);
		}else{
			$res=array("code"=>400,'msg'=>"");
		}
		
		echo json_encode($res);
		exit;
	}

	/*
	/**
	 * 修改支付密码
	 */
	public function update_pay_pwd(){
		$member=model("member")->find(input("uid"));
		if(md5_salt(input('old_pwd'))!=$member['pay_password']){
			return $this->toJson(408, '旧密码输入错误');
		}else{
			$res=$member->where("id",input("uid"))->update(["pay_password"=>md5_salt(input("new_pwd"))]);
			if($res){
				return $this->toJson(200, '修改成功', $member);
			}else{
				return $this->toJson(408, '修改失败');
			}
		}
	}
	/**
	 * POST
	 * 会员注册
	 */
	public function regist(){
    	$member_model = new MemberModel();
		$member=$member_model->where('mobile',input('mobile'))->find();
		if($member){
			return $this->toJson(406, '该手机号码已经注册'); 
		}
		$member_model->login_time = now_time();
		$member_model->mobile = input('mobile');
		$member_model->password=md5_salt(input('login_pwd'));
		$member_model->login_ip = $this->request->ip();
		$result = $member_model->allowField(true)->save();
		if($result){
			return $this->toJson(200, '注册成功', $member_model);
		}else{
			return $this->toJson(406, '注册失败');               
		}
	}
	/**
	 * 我的推广中心
	 */
	/*public function tgzx(){
		$uid=input('uid');
		$shareUrl =config('DOMAIN')."/member/login.html?fromuid={$uid}&from=singlemessage&isappinstalled=0";
		$code = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
		$field = 'm.*,g.uid,g.level,g.son_id';
		$where['g.uid']=$uid;
		$list=Db::name("mall_level")->alias('g')->join('__MEMBER__ m','m.id=g.son_id')->field($field)->where($where)->order("m.add_time desc")->select();
		$data=[];
     	foreach($list as $k){
     		$data[$k['level']][]=$k;
     	}
		$data['code_url']=$code;
		return $this->toJson(200,'ok',$data);
	}*/
	
	
	
	
	
	/**
	 * 我的钱包
	 */
	public function myWallet(){
		$where['uid']=input('uid');
		$where['user_type']=1;
		$where['cash_status']=2;
		$finish=db('mall_cashmanage')->where($where)->sum("balance");//已完成
		$where['cash_status']=1;
		$txing=db('mall_cashmanage')->where($where)->sum("balance");//提现中
		return json(['finish'=>$finish,'txing'=>$txing]);
	}
	/**
	 * 我的消费记录
	 */
	public function xiaofei(){

		$xiaofei=Db::name('mall_paylog')->where('uid',input('uid'))->select();//已完成

		return json(['xiaofei'=>$xiaofei]);
	}
	
	
	/**
	 * 我的推广中心
	 */
	public function tgzx(){
		$uid=input('uid');
		$shareUrl =config('DOMAIN')."/member/login.html?fromuid={$uid}&from=singlemessage&isappinstalled=0";
		$code = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
		$field = 'm.*,g.uid,g.level,g.son_id';
		$where['g.uid']=$uid;
		
		$list1=Db::name("mall_level")->alias('g')->join('__MEMBER__ m','m.id=g.son_id')->field($field)->where($where)->order("m.add_time desc")->select();
     	foreach($list1 as $key=>$value){
     		$level2_ids[] = $list1[$key]['id'];
     	}
     	$list2 = Db::name("mall_level")->alias('g')->join('__MEMBER__ m','m.id=g.son_id')->field($field)->where('g.uid','in',$level2_ids)->order("m.add_time desc")->select();
		
		foreach($list2 as $key=>$value){
     		$level3_ids[] = $list2[$key]['id'];
     	}
		
		$list3 = Db::name("mall_level")->alias('g')->join('__MEMBER__ m','m.id=g.son_id')->field($field)->where('g.uid','in',$level3_ids)->order("m.add_time desc")->select();
		
		$data['code_url']=$code;
		$data[1] = $list1;
		$data[2] = $list2;
		$data[3] = $list3;
		
		//dump($data);
		return $this->toJson(200,'ok',$data);
	}
	
	/**
	 * 我的提现记录
	 */
	public function tixian(){
		$where['uid']=input('uid');
		$where['user_type']=1;
		$txlist=db('mall_cashmanage')->where($where)->select();
		return json(['txlist'=>$txlist]);
	}
	/**
	 * POST
	 * 重置密码
	 */
	public function return_pwd(){
			$data = input();
		    $code = $data['verifycode'];
			$result = check_sendcode($data['mobile'], $code, '找回密码');
			if ($result != 1) {
				return $this->toJson(408, '验证码错误或过期！');
			}
        	$member = Db::name('member')->where('mobile='.$data['mobile'])->find();
        	if($member){
	        	$data['password'] = md5_salt($data['login_pwd']);
				
				$result = Db::name('member')->where('id', $member['id'])->update(['password' => $data['password']]);
				if($result){
					return $this->toJson(200, '修改成功');
				}else{
					return $this->toJson(406, '修改失败');               
				}
       		}else{
       			return $this->toJson(408,'用户不存在!');
       		}
	}
	
	/**
	 * POST
	 * 修改密码
	 */
	public function update_pwd(){
		if($this->request->isPost()){
			$data = $this->request->param();
		    $code = $data['verifycode'];
			$result = check_sendcode($data['mobile'], $code, '找回密码');
			if ($result != 1) {
				return $this->toJson(408, '验证码错误或过期！');
			}
			$where['mobile'] = $data['mobile'];
			$where['password'] = md5_salt($data['old_pwd']);
        	$member = Db::name('member')->where($where)->find();
        	if(!$member){
        		return $this->toJson(408,'用户不存在!');
       		}
        	$member_model = new MemberModel();
        	$data['password'] = md5_salt($data['new_pwd']);
			
			$result = Db::name('member')->where('id', $member['id'])->update(['password' => $data['password']]);
			//$result = $member_model->allowField(true)->save($data);
			if($result !== false){
				$member = $member_model->where('id='.$result)->find();
				return $this->toJson(200, '修改成功');
			}else{
				return $this->toJson(406, '修改失败');               
			}
            
		}
	}
	protected function check_code($mobile, $code, $message){
		$where['mobile'] = $mobile;
		$where['code'] = $code;
		$where['message'] = '发送成功';
		$result =Db::name('sms_code')->where($where)->find();
		// 10分钟有效期
		if(time() - strtotime($result['add_time']) > 600){
			return 2;
		}
		if($code == $result['code']){
			return 1;
		}else{
			return -1;
		}
	}
	/**
	 * 设置用户提现账号信息
	 */
	public function sjtixian(){
		$data=input("post.");
		$id=$data['id'];
		if($id !="" ){
				$res=Db::name('mall_cash')->where('id', $id)->update(['bank_account'=>$data['bank_account'],'card_no'=>$data['card_no'],'bank_username'=>$data['bank_username']]);
		
		}else{
		 	unset($data['id']);
			$data1=['uid'=>$data['uid'],'bank_account'=>$data['bank_account'],'card_no'=>$data['card_no'],'bank_username'=>$data['bank_username'],'type'=>$data['type'],'user_type'=>$data['user_type'],'add_time'=>$data['add_time']];
			$res=Db::name('mall_cash')->insert($data1);
		}
		if($res){
	        	return $this->toJson(200,'保存成功');
      }else{
			return $this->toJson(405,'保存失败');               
      }
	}
	
	/**
	 * 根据ID获取会员Model
	 */
	public function get($id){
		if($id){
			$member = Db::name('member')->where('id='.$id)->find();
	        if($member){
	        	return $this->toJson(200,'',$member);
	        }else{
				return $this->toJson(405,'数据不存在');               
	        }
		}
	}
	
	public function uploadimg(){
		$this->upload('member');
	}
	
	/**
	 * 管理收货地址
	 */
	public function getAddressList(){
		$uid = request() -> param('uid');
		$addresslist = Db::name('mall_address')->where('uid='.$uid)->select();
		if($addresslist){
			return $this->toJson(200, 'ok', $addresslist);
		}else{
			return $this->toJson(405, '',"数据异常");
		}
	}

	/**
	 * 需要修改的收货地址
	 */
	public function update_AddressList(){
		$id = request() -> param('id');
		$addresslist = Db::name('mall_address')->where('id='.$id)->find();
		if($addresslist){
			return $this->toJson(200, 'ok', $addresslist);
		}else{
			return $this->toJson(405, '',"数据异常");
		}
	}
	
	/**
	 * 修改地址
	 */
	public function update_address() {
		$data['uid'] = request() -> param('uid');
		$data['realname'] = request() -> param('realname');
		$data['province'] = request() -> param('province');
		$data['city'] = request() -> param('city');
		$data['district'] = request() -> param('district');
		$data['address'] = request() -> param('address');
		$data['mobile'] = request() -> param('mobile');
		$data['isdefault'] = request() -> param('isdefault'); 		
		$id = request() -> param('id');
		if (Db::name('mall_address')->where('id='.$id)->update($data)) {
			return $this -> toJson('200', '保存成功');
		} else {
			return $this -> toJson('406', '保存失败');
		}
		
	}
	/**
	 * 删除收货地址
	 */
	public function del_address(){		
		$id = request() -> param('id');
		$json = db('mall_address') -> where("id=" . $id) -> delete();
		if ($json) {
			return $this -> toJson('200', '删除成功');
		} else {
			return $this -> toJson('406', '删除失败');
		}				
	}
	/**
	 * 我的关注列表
	 */
	/*public function getCollect(){		
		$uid = request() -> param('uid');
		$list=Db::name('mall_collect')->where('uid='.$uid)->select();
		for($i = 0; $i < count($list); $i++) {
			if($list[$i]['collect_type']==1){
				$d_collectlist = Db::name('mall_collect')->alias('a')
					   ->join('member m','a.uid=m.id')
					   ->join('mall_agent g','a.collect_id=g.id')
					   ->field('a.*,g.thumb,g.store_name,g.address')
					   ->where('uid='.$uid)->where('collect_type',1)->select();
			}
			if($list[$i]['collect_type']==2){
				$g_collectlist = Db::name('mall_collect')->alias('a')
					   ->join('member m','a.uid=m.id')
					   ->join('line_goods g','a.collect_id=g.id')
					   ->field('a.*,g.thumb,g.goods_name,g.goods_no')
					   ->where('uid='.$uid)->where('collect_type',2)->select();
			}
		}
		$data[]=$d_collectlist;
		$data[]=$g_collectlist;
		if($d_collectlist || $g_collectlist){
			return $this->toJson(200, 'ok', $data);
		}else{
			return $this->toJson(405, '',"数据异常");
		}		
	}*/
	
	
	/*
	 * 显示关注的店铺
	 */
	public function getCollect(){		
		$uid = request() -> param('uid');
		
		$d_collectlist = Db::name('mall_collect')->alias('a')
			   ->join('member m','a.uid=m.id')
			   ->join('mall_agent g','a.collect_id=g.id')
			   ->field('a.*,g.thumb,g.store_name,g.address')
			   ->where('uid='.$uid)->where('collect_type',1)->select();

		if($d_collectlist){
			return $this->toJson(200, 'ok', $d_collectlist);
		}else{
			return $this->toJson(405, '',"数据异常");
		}		
	}
	
	/*
	 * 显示关注的商品
	 */
	public function getCollect2(){		
		$uid = request() -> param('uid');
		
		$g_collectlist = Db::name('mall_collect')->alias('a')
			   ->join('member m','a.uid=m.id')
			   ->join('line_goods g','a.collect_id=g.id')
			   ->field('a.*,g.thumb,g.goods_name,g.goods_no')
			   ->where('uid='.$uid)->where('collect_type',2)->select();

		if($g_collectlist){
			return $this->toJson(200, 'ok', $g_collectlist);
		}else{
			return $this->toJson(405, '',"数据异常");
		}		
	}
	/*
	 * 删除关注
	 */
	public function delete_Collect(){
		$id = request() -> param('id');
		$json = db('mall_collect') -> where("id=" . $id) -> delete();
		if ($json) {
			return $this -> toJson('200', '取消成功');
		} else {
			return $this -> toJson('406', '取消失败');
		}	
	}
     

	/**
	 * 获取用户提现方式
	 */
	public function gettixian(){
		 $tx=db("mall_cash")->where("uid",input("uid"))->find();
	     return $this->toJson(200,'',$tx);
	}
	
	
	/**
	 * 添加地址
	 */
	public function address_add() {
			$data['uid'] = request() -> param('uid');
			$data['realname'] = request() -> param('realname');
			$data['province'] = request() -> param('province');
			$data['city'] = request() -> param('city');
			$data['district'] = request() -> param('district');
			$data['address'] = request() -> param('address');
			$data['mobile'] = request() -> param('mobile');
			$data['isdefault'] = request() -> param('isdefault');
			$uid = request() -> param('uid');
			$count = Db::name('mall_address')-> where('uid', $uid) -> count();
			if ($count >= 5) {
				return $this -> toJson('408', '添加地址最多为5个');
			}
			if ($count < 1) {
				$data['isdefault']=2;
			}
			$id = request() -> param('id');
			if($data['isdefault']==2){
				$old_address = Db::name('mall_address')-> where('uid', $data['uid']) ->where('isdefault',2)->update(['isdefault' => 1]);
			}
			if (Db::name('mall_address')->insert($data)) {
				return $this -> toJson('200', '添加成功');
			} else {
				return $this -> toJson('406', '添加失败');
			}
	}


	/**
	 * 我的关注列表
	 */
	/*public function getCollect(){		
		$uid = request() -> param('uid');
		$addresslist = Db::name('mall_collect')->alias('a')
					   ->join('member m','a.uid=m.id')
					   ->join('mall_agent g','a.agent_id=g.id')
					   ->field('a.*,g.thumb,g.store_name,g.address')
					   ->where('uid='.$uid)->select();
		if($addresslist){
			return $this->toJson(200, 'ok', $addresslist);
		}else{
			return $this->toJson(405, '',"数据异常");
		}		
	}*/
	
	
	/**
	 * 提交反馈 
	 */
	function add_complaint(){
		$param = request()->param();
		$param['complaint_type'] = $param['type'];
		$param['addtime'] = now_time();
		$param['status'] = 1;
		
		$complaint = new  ComplaintModel(); 
		$result = $complaint->allowField(true)->save($param);

		if($result){
			return $this->toJson(200,'提交成功！');
		}else{
			return $this->toJson(405,"提交失败！");
		}	
	}
	
	/**
	 * 帮助中心
	 */
	function get_helps(){
		
		$auth_rule_list = db('mall_helptype')->select();
        foreach ($auth_rule_list as $value) {
                $menu[] = $value;
        }
        $menu = !empty($menu) ? array2tree($menu) : [];

		if($menu){
			return $this->toJson(200,'',$menu);
		}else{
			return $this->toJson(405,"数据异常！");
		}	
	}
	
	/*
	 * 帮助详情
	 */
	function get_help(){
		
		$help_type = db('mall_helptype')->where('id',input('help_id'))->find();
		$detail = db('mall_helps')->where('pid',input('help_id'))->find();
		
		$detail['content'] = htmlspecialchars_decode($detail['content']);
		
		$cut_help['help_type'] = $help_type;
		$cut_help['detail'] = $detail;
		
		return $this->toJson(200,'',$cut_help);
	}
		
		
}
